package com.smarthospital.common.util;

import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;
import com.smarthospital.entity.pojo.SHDictionary;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * create by sym  2021/5/24 20:57.
 */
public class SHJSONUtil {

    public static Map DictionaryToMap(List<SHDictionary> shDictionaries) {
        //校验所有字典对象的 字典 一样
        if (CollectionUtils.isEmpty(shDictionaries)) {
            return new HashMap();
        }
        String dictionary = shDictionaries.get(0).getDictionary();
        if (!shDictionaries.stream().allMatch(d->d.getDictionary().equals(dictionary))) {
            throw new CommonException(CodeMsg.ERROR_DICTIONARY);
        }
        Map<String, String> map = new HashMap<>(shDictionaries.size());
        for (SHDictionary shDictionary : shDictionaries) {
            map.put(shDictionary.getKey(),shDictionary.getValue());
        }
        return map;
    }
}
